/**
 *  Copyright Matt Silbernagel 2010
 *  
 *	This file is part of MobilePrinting.
 *
 *	MobilePrinting is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MobilePrinting is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MobilePrinting.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.uc.labs.MobilePrinting.PrintUtils;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

import edu.uc.labs.MobilePrinting.Configurator;

public class PrinterValidator {
	
	private static Logger logger = Configurator.LOGGER;
	
	public static boolean validate( String file_to_validate, String validator_file ){
		
		String printerFile = Configurator.USERSPACE + Configurator.XMLFILE;
		String s		   = Configurator.USERSPACE + Configurator.XMLVALIDATE;
		
		SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
		File schemaLocation = new File( s );
		try {
			Schema schema = factory.newSchema( schemaLocation );
			Validator validator = schema.newValidator();
			Source source = new StreamSource( printerFile );
			validator.validate(source);
			logger.error("Validation on " + printerFile + " succeded!");
			return true;
		} catch (SAXException e) {
			logger.error("Validation on " + printerFile + " failed with error message: ");
			logger.error(e.toString());
			return false;
		} catch (IOException e) {
			logger.error("Validation on " + printerFile + " failed with error message: ");
			logger.error(e.toString());
			return false;
		}
	}
	
}
